package com.topvision.jack.camerasdk2.camera.utils;


import android.content.Context;
import android.graphics.Point;
import android.hardware.Camera;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.topvision.jack.camerasdk2.R;
import com.topvision.jack.camerasdk2.admin.TvCameraSdk;
import com.topvision.jack.camerasdk2.camera.commom.CameraConstants;

/**
 * Camera 工具类
 * 创建时间:2017/12/20
 * 作者 jack
 * 描述
 */
public class CameraUtils {

    public static final String TAG = TvCameraSdk.class.getSimpleName();

    /**
     * 设置设备最优的预览分辨率
     * 创建时间:2017/12/20
     * 作者 jack
     * 描述
     */
    public static Camera.Size findBestPreviewSize(Context context, Camera mCamera, Camera.Parameters parameters) {
        int diff = Integer.MAX_VALUE;
        String previewSizeValueString = parameters.get("preview-size-values");
        if (previewSizeValueString == null) {  // 有些手机例如m9获取不到支持的预览大小   就直接返回屏幕大小
            return mCamera.new Size(getScreenWH(context).widthPixels, getScreenWH(context).heightPixels);
        }
        Log.d(TAG, "previewSizeValueString : " + previewSizeValueString);
        int bestX = 0;
        int bestY = 0;

        for (String prewsizeString : previewSizeValueString.split(",")) {
            prewsizeString = prewsizeString.trim();

            int dimPosition = prewsizeString.indexOf('x');
            if (dimPosition == -1) {
                continue;
            }

            int newX = 0;
            int newY = 0;

            try {
                newX = Integer.parseInt(prewsizeString.substring(0, dimPosition));
                newY = Integer.parseInt(prewsizeString.substring(dimPosition + 1));
            } catch (NumberFormatException e) {
                continue;
            }

            Point screenResolution = new Point(getScreenWH(context).widthPixels, getScreenWH(context).heightPixels);

            int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);

            if (newDiff == diff) {
                bestX = newX;
                bestY = newY;
                break;
            } else if (newDiff < diff) {
                if ((3 * newX) == (4 * newY)) {
                    bestX = newX;
                    bestY = newY;
                    diff = newDiff;
                }
            }
        }
        if (bestX > 0 && bestY > 0) {
            return mCamera.new Size(bestX, bestY);
        }
        return null;
    }

    protected static DisplayMetrics getScreenWH(Context context) {
        DisplayMetrics dMetrics = context.getResources().getDisplayMetrics();
        return dMetrics;
    }

    /**
     * 判断是否支持该对焦模式
     *
     * @param parameters 参数对象
     * @param focusMode  对焦模式
     * @return
     */
    public static boolean isSupportFocusMode(Camera.Parameters parameters, String focusMode) {
        for (String mode : parameters.getSupportedFocusModes()) {
            if (mode.equals(focusMode)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isSupportPreviewSize(Context context, Camera.Parameters parameters, int width, int height) {
        String previewSizeValueString2 = parameters.get("preview-size-values");
        Log.e(CameraConstants.TAG, context.getString(R.string.camera_preview_size_support) + " " + previewSizeValueString2);
        for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
            if (size.width == width && size.height == height) {
                return true;
            }
        }
        Log.e(CameraConstants.TAG, context.getString(R.string.camera_preview_size_not_support) + " " + width + "x" + height);
        String previewSizeValueString = parameters.get("preview-size-values");
        Log.e(CameraConstants.TAG, context.getString(R.string.camera_preview_size_support) + " " + previewSizeValueString);
        return false;
    }

    public static boolean isSupportPictureSize(Context context, Camera.Parameters parameters, int width, int height) {
        for (Camera.Size size : parameters.getSupportedPictureSizes()) {
            if (size.width == width && size.height == height) {
                return true;
            }
        }

        Log.e(CameraConstants.TAG, context.getString(R.string.camera_picture_size_not_support) + " " + width + "x" + height);
        String pictureSizeValueString = parameters.get("picture-size-values");
        Log.e(CameraConstants.TAG, context.getString(R.string.camera_picture_size_support) + " " + pictureSizeValueString);
        return false;
    }

    /**
     * 获取屏幕分辨率
     *
     * @author jack
     * create at 2016/11/21 16:00
     */
    public static Point getScreenPoint(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Point screenResolution = new Point(display.getWidth(), display.getHeight());
        return screenResolution;
    }
}
